


//
//  CPJVisitorView.swift
//  Sina
//
//  Created by YYY on 2017/7/27.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

private let edge = (kScreenWidth - 200)/3.0

class CPJVisitorView: UIView {

    //注册
    lazy var registBtn: UIButton = UIButton(frame: CGRect(x: edge, y: 0, width: 100, height: 30), titleStr: "注册", titleColor: UIColor.orange, fontSize: 15,isBorder: false)
    //登录
    lazy var loginBtn: UIButton = UIButton(frame: CGRect(x: 2*edge + 100, y: 0, width: 100, height: 30), titleStr: "登录", titleColor: UIColor.darkGray, fontSize: 15,isBorder: false)
    
    var visitorDict: [String: String]?
    {
        didSet
        {
            guard let message = visitorDict?["message"],let iconImage = visitorDict?["iconImage"]  else
            {
                return
            }
            tipLabel.text = message
            if iconImage == ""
            {
                startAnimation()
                return
            }
            loadingView.image = UIImage(named: iconImage)
            loadingView.frame = CGRect(x: (kScreenWidth-200)/2.0, y: 120, width: 200, height: 200)
            houseView.isHidden = true
        }
    }
    
    //构造函数
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //首页的旋转动画
    private func startAnimation()
    {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * Double.pi
        animation.repeatCount = MAXFLOAT
        animation.duration = 15.0
        //动画完成不删除，如果loadingView被释放，动画会一起销毁
        animation.isRemovedOnCompletion = false
        
        loadingView.layer.add(animation, forKey: nil)
    }
    
    

    //私有控件
    //加载
    fileprivate lazy var loadingView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //房子
    fileprivate lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示
    fileprivate lazy var tipLabel: UILabel = UILabel(frame: CGRect(x: 15, y: 0, width: kScreenWidth-30, height: 30), text: "新浪微博，开启你的生活", textColor: UIColor.darkGray, fontsize: 14)
}

extension CPJVisitorView
{
    func setUpUI()
    {
        backgroundColor = UIImage.hexString(toColor: "0XEDEDED")
        //代码布局
        loadingView.frame = CGRect(x: (kScreenWidth-250)/2.0, y: 100, width: 250, height: 250)
        addSubview(loadingView)
        
        addSubview(houseView)
        
        houseView.frame = CGRect(x: 0, y: 0, width: 188*0.6, height: 180*0.6)
        houseView.center = loadingView.center
        
        addSubview(tipLabel)
        tipLabel.height(newHeight: 60)
        tipLabel.textAlignment = .center
        tipLabel.numberOfLines = 0
        tipLabel.top(newTop: loadingView.top + loadingView.height)
        
    
        registBtn.top(newTop: tipLabel.bottom + 10)
        registBtn.setTitleColor(UIColor.darkGray, for: .highlighted)
        registBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        addSubview(registBtn)
        
        loginBtn.top(newTop: registBtn.top)
        loginBtn.setTitleColor(UIColor.orange, for: .highlighted)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        addSubview(loginBtn)
    }
}
